package com.zuikaku.controller;

import com.zuikaku.dao.OrderDao;
import com.zuikaku.dao.UserDao;
import com.zuikaku.pojo.Order;
import com.zuikaku.pojo.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserController extends DispatchServlet{


    public String login(HttpServletRequest request, HttpServletResponse response){
        String inputUsername = request.getParameter("username");
        String inputPassword = request.getParameter("password");
        User user = UserDao.getUserByUserNameAndPassword(inputUsername,inputPassword);
        if(user==null){
            //todo 跳转错误页面
            String msg = "账户不存在或者密码错误";
            request.setAttribute("msg",msg);
            return "/WEB-INF/page/error.jsp";
        }else{
            HttpSession session =request.getSession();
            session.setAttribute("activeUser",user);
            ServletContext servletContext = session.getServletContext();
            servletContext.setAttribute("notice","周5不上课");
            return "redirect:/user?method=home";

        }
    }


    public String home(HttpServletRequest request, HttpServletResponse response){
        User activeUser =(User) request.getSession().getAttribute("activeUser");
        List<Order> orderList = OrderDao.getOrderByUserId(activeUser.getUserId());
        request.setAttribute("orderList",orderList);
        return "/WEB-INF/page/home.jsp";
    }
}
